<?php
namespace App\Controllers\Task;
/**
*	修改，添加任务
 *
*/
use App\Models\Task\Task;
use App\Libs\Views\Json;
class CreateAlterTask extends \App\Libs\Base\BaseAction
{
    private $mapField = [
        'task_title'    => 'task_title',
        'task_icon'     => 'task_icon',
        'task_banner'   => 'task_banner',
        'task_intro'    => 'task_intro',
        'odscore'       => 'order_score',
        'isshow'        => 'show',
    ];
	
	public function execute(){

        if($this->request->isPost()){
            if($this->request->hasPost('task_id')){
                $taskID = $this->request->getPost('task_id');
                $matchTask = Task::findOne($taskID);
                $matchTask->utime = date('Y:m:d H:i:s');
            }else{
                $matchTask = new Task();
                $matchTask->ctime = date('Y:m:d H:i:s');
                $matchTask->utime = date('Y:m:d H:i:s');
            }
            $post = $this->request->getPost();
            foreach ($this->mapField as $key => $val){
                if(isset($post[$val])){
                    $matchTask->{$key} = $post[$val];
                }
            }
            if(!$matchTask->save()){
                return Json::view(1,'失败，请重试',[]);
            }else{
                return Json::view(0,'操作成功',[]);
            }
        }else{
            if(!$this->request->has('task_id')){
                return Json::view(1,'缺少任务id：task_id',NULL);
            }
            $task = $this->request->getQuery('task_id');
            $matchTask = Task::findOne($task);
            $newData = [];
            foreach ($this->mapField as $key => $val){
                if(isset($matchTask->{$key})){
                    $newData[$val] = $matchTask->{$key};
                }
            }
            $newData['id'] = $matchTask->id;
            return \App\Libs\Views\Json::view(0, '成功', $newData);
        }


    }

}
